<template>
	<view>
		<view style="height: 70upx;">
		</view>
		<view :class='["calendar",hideEdge,viewType+"-view"]'>
			<view class='week-label-row' :class="loginType==2?'Vipztrli':''">
				<!-- :class="item == '六'||item == '日'?'colorred':''"  -->
				<view v-for='(item,index) in weekLabel' :key="index">
					{{item}}
				</view>
			</view>
			<swiper circular :duration="200" :data-data="views" :current="currentIndex" @change="swiperChange"
				@animationfinish="swiperFinish" @transitionend="transitionEnd" @touchstart="startTouch"
				@touchmove="moveTouch" @touchend="endTouch" @touchcancel="cancelTouch" :class="animate">
				<block v-for="(item,idx) in views" :key="idx">
					<swiper-item>
						<view @tap="suoxiao" :class="loginType==2?'Vipztrli':''" class='grid'
							:style='"top:"+topValues[idx]+"rpx"'>
							<view v-for="(dayItem,dayIndex) in item" :key='dayIndex'
								@tap='selectDate(dayItem.label,dayItem)' :class="['item item-'+dayItem.type,viewIndex==idx && currentDate==dayItem.date ? 'selected':''
							 ,'item','item-'+dayItem.date==dayItem.jintian? 'sens':'']">
								<view class='inner'
									:class="[indicator[dayItem.date] ? 'indicator indicator-' + themeColors[indicator[dayItem.date]] :'']">
									<view class="dateStyle">
										{{dayItem.day}}
									</view>
								</view>
							</view>
						</view>
					</swiper-item>
				</block>
			</swiper>
		</view>
		<!-- <view class="Unfoldandstow" @tap="openHideTab(viewType)"> -->
		<view class="Unfoldandstow">
			<!-- <view class="openshwo">
			</view> -->
		</view>
	</view>
</template>

<script>
	import Calendar from "./calendar.js"
	import WxTouch from "./wx-touch.js"
	import festival from "./festival.js";
	import mif from './mif';

	function sswitch(date) {
		let arrdata = date.split("-")
		let number = parseInt(arrdata[1]),
			number1 = parseInt(arrdata[2]);
		if (number1 <= 9) {
			arrdata[2] = '0' + arrdata[2]
		}
		if (number <= 9) {
			arrdata[1] = '0' + arrdata[1]
			arrdata = arrdata.join('-')
			// arrdata = parseInt(arrdata)
		} else {
			arrdata = arrdata.join('-')
			// arrdata = parseInt(arrdata)
		}
		return arrdata
	}
	// import time from '@/basejs/time';
	export default {
		props: {
			// 指示灯
			indicator: {
				type: Object,
				default: Object
			},
		},
		data() {
			return {
				viewType: 'week',
				loginType: 1,
				sessionid: '',
				ltop: 0,
				xiu: [], //休数组
				luckarr: [], //吉数组
				fierce: [], //忌数组
				weekLabel: ["日", "一", "二", "三", "四", "五", "六"],
				views: [], // 视图组
				currentIndex: 0, // 当前索引，控制 swiper
				viewIndex: 0, // 当前索引，不控制 swiper
				hideEdge: "", // 是否隐藏边缘，因为 42 格子的周视图，会漏出相邻日期的激活状态样式
				topValues: [0, 0, 0], // 控制周视图的隐藏高度
				currentDate: "", // 标记今天
				animate: "", // 切换周月视图时开启动画过渡
				sfsowx: "", //是否启动动画
				qujuassd: "",
			}
		},
		/**
		 * 初始化控制器
		 */
		created() {
			this.calendar = new Calendar();
			let thisd = this;
			this.watchValue(this.value);
			this.calendar.watchValue = true;
			uni.$on('cease', function(e) { //接收休
				if (thisd.calendar.watchValue && e.thisday != thisd.currentDate) {
					thisd.watchValue(e.thisday);
				}
				// thisd.currentDate = e.thisday;
				let loginType = uni.getStorageSync('loginType');
				// console.log(e, 100)
				thisd.xiu = e.arr;
				thisd.fierce = e.fierce;
				thisd.luckarr = e.luckarr;
				thisd.loginType = loginType;

				// if(uni.getStorageSync('rilikjIshd')){
				// 	thisd.watchValue(e.thisday);
				// 	this.calendar.watchValue = true;
				// }
			})
			uni.$on('huangliShow', function(e) {
				let setData = {},
					data = thisd,
					trigger = false,
					calendar = thisd.calendar;
				if (e == true) {
					thisd.views = calendar.refreshWeekViews(data.currentDate).views;
					thisd.topValues = calendar.refreshWeekViews(data.currentDate).topValues
					data.viewType = "week";
					trigger = true;
				} else {
					thisd.views = calendar.refreshMonthViews(calendar.currentMonth).views
					// setData = calendar.refreshMonthViews(calendar.currentMonth);
					data.viewType = "month";
					thisd.topValues = [0, 0, 0];
					trigger = true;
				}
			})
		},
		methods: {
			/**
			 * 监听 swiper 改变
			 */
			openHideTab(e) {
				let thisd = this;
				let setData = {},
					data = thisd,
					trigger = false,
					calendar = thisd.calendar;
				if (e == 'month') {
					thisd.views = calendar.refreshWeekViews(data.currentDate).views;
					thisd.topValues = calendar.refreshWeekViews(data.currentDate).topValues
					data.viewType = "week";
					trigger = true;
				} else {
					thisd.views = calendar.refreshMonthViews(calendar.currentMonth).views
					// setData = calendar.refreshMonthViews(calendar.currentMonth);
					data.viewType = "month";
					thisd.topValues = [0, 0, 0];
					trigger = true;
				}
			},
			swiperChange(evt) {
				let index = evt.detail.current,
					calendar = this.calendar,
					// 根据视图类型使用不同的刷新机制
					data = this.viewType == "week" ?
					calendar.changeWeekViews(index) :
					calendar.changeMonthViews(index);
				// 更新视图索引的标记，仅仅是标记
				this.viewIndex = index;
				// let assignment =  getFirst(data);
				let assignment = Object.entries(data)
				let views = assignment[0],
					topValues = assignment[1],
					viewsdata = this.views,
					topValuesdata = this.topValues;
				// console.log(viewsdata,topValuesdata,topValues[1],123)
				if (views[0] == "views[0]") {
					viewsdata[0] = views[1];
					if (topValues) {
						topValuesdata[0] = topValues[1];
					}
				} else if (views[0] == "views[1]") {
					viewsdata[1] = views[1];
					if (topValues) {
						topValuesdata[1] = topValues[1];
					}
				} else if (views[0] == "views[2]") {
					viewsdata[2] = views[1];
					if (topValues) {
						topValuesdata[2] = topValues[1];
					}
				}
				this.viewsdata = viewsdata;
				if (topValues) {
					this.topValuesdata = topValuesdata
				}
				// 保存当前索引到实例
				calendar.index = index;

				// 标记视图已经发送变化
				calendar.changed = true;
			},
			/**
			 * swiper 滑动结束
			 */
			swiperFinish(evt) {
				let calendar = this.calendar,
					t = this,
					arr0 = evt.currentTarget.dataset.data[0],
					arr1 = evt.currentTarget.dataset.data[1],
					arr2 = evt.currentTarget.dataset.data[2],
					arr = [];
				// 当视图发送变化，才会执行
				if (calendar.changed) {
					console.log(calendar, 188)
					let isdayMaxmin = this.compareDate(calendar.currentWeek, this.$basejs.thisdayFormat2(false))
					if (isdayMaxmin) { //选择日期大于今天
						uni.showToast({
							title: '仅查看当天和以往健康任务',
							icon:'none',
							duration: 3000
						});
						return
					}
					// 滑动结束后，自动设置日期
					if (evt.detail.source == "touch") {
						this.setDate(this.viewType == "week" ? calendar.currentWeek : "auto");
						let arrdata = '' + sswitch(calendar.currentMonth + '-' + calendar.currentDay);
						this.$emit('queryindex', arrdata)
					}
					// 重置标记
					calendar.changed = false;
				}
			},
			// 切换的过渡完成后，是否隐藏边缘
			// 当为周视图的时候，边缘可能会漏出其他日期的样式
			transitionEnd() {
				// this.hideEdge = this.viewType == "week" ? "hide-edge" : ""
			},
			compareDate(s1, s2) {
				return ((new Date(s1.replace(/-/g, "\/"))) > (new Date(s2.replace(/-/g, "\/"))));
			},
			// 点击选择某个日期
			selectDate(id, item) {
				console.info(id, item, "--------------------->")
				let isdayMaxmin = this.compareDate(item.date, this.$basejs.thisdayFormat2(false))
				if (isdayMaxmin) { //选择日期大于今天
					uni.showToast({
						title: '仅查看当天和以往健康任务',
						icon:'none',
						duration: 3000
					});
					return
				}
				// console.log(isdayMaxmin,23555555)
				// console.log(id, item)
				console.log('娄你大也')
				let dayrq = item.dayrq,
					dataobj = '';
				let data = {},
					t = this,
					date = item.date,
					calendar = this.calendar,
					isWeekView = this.viewType == "week";
				// let daylidh = true;
				// onfire.fire('daylidh', daylidh);
				this.qujuassd = item;
				let arrdata = '' + sswitch(date)
				this.$emit('queryindex', arrdata)
				// uni.$emit('queryindex', arrdata)
				let month = ''
				// 发送年月日
				if (item.type != "current") {
					// 如果选择的不是当前视图月份的日期，那么就切换
					// 点击上一个月或者下一个月的日期，不会产生动画过渡，但是数据会变化
					if (isWeekView) {
						data = calendar.refreshWeekViews(date);
						data.animate = "";
					} else {
						// 切换视图位置，点击上一个月或者下一个月的日期，会产生过渡变化
						data.currentIndex = calendar.SIBLING[calendar.index][item.type];
					}
					this.currentIndex = data.currentIndex
					// this.setData(data);
				} else {

				}

				// 周视图下选择日期，要同时更新下一个星期和上一个星期的值
				if (isWeekView) {
					calendar.prevWeek = calendar.getDiffDate(date, -7);
					calendar.nextWeek = calendar.getDiffDate(date, 7);
				}

				// 保存当前选择的 “日”
				calendar.currentDay = date.split("-")[2];

				this.setDate(date);
			},

			/**
			 * 设置激活日期状态，上传事件
			 */
			setDate(date) {
				if (date == "auto") {
					let calendar = this.calendar;

					// 自动灵活设置日期
					// 比如现在选择的是31号，切换到2月份，会自动选择 28 或者 29，因为2月份没有31号
					if (calendar.currentDay > calendar.currentView.totalDay) {
						calendar.currentDay = calendar.currentView.totalDay;

					}
					date = calendar.currentMonth + "-" + calendar.currentDay;

				}
				// console.log(date, 299)
				this.currentDate = date

				if (this.calendar.watchValue) {
					// 上传 change 事件
					this.value = date
					// this.triggerEvent("change", {
					// 	value: date
					// })
				}
			},
			/**
			 * 监听 value 值变化
			 */
			watchValue(date) {
				let calendar = this.calendar,
					value = calendar.getValue(date);
				// 根据视图类型使用不同的刷新机制
				// this.viewType == "month" ?
				// 	calendar.refreshMonthViews(value.month) :
				// 	calendar.refreshWeekViews(value.date)
				let valueData = this.viewType == "month" ?
					calendar.refreshMonthViews(value.month) :
					calendar.refreshWeekViews(value.date);
				if (this.viewType == "month") {
					this.topValues = [0, 0, 0]
				} else {
					this.topValues = valueData.topValues;
				}
				this.views = valueData.views;
				// console.log(valueData,344)
				this.setDate("auto");
			},
			/**
			 * 自定义手势事件+点击事件
			 */
			suoxiao(evta) {
				let burre = this.qujuassd.type;
				if (burre === "current") {
					let setData = {},
						data = this,
						trigger = false,
						calendar = this.calendar;
					this.animate = "animate";
					this.topValues = calendar.refreshWeekViews(data.currentDate).topValues;
					this.viewType = "week";
					this.views = calendar.refreshWeekViews(data.currentDate).views;
					// this.viewType = setData.viewType
				}
			},
			...WxTouch("Touch", {

				swipeVelocity: false,

				pressmove(evt) {
					this.calendar.pressmoved = true;
				},

				swipe(evt) {
					let setData = {},
						data = this,
						trigger = false,
						calendar = this.calendar,
						thisd = this;
					if (calendar.pressmoved) {
						// 向上滑动更新为 “周” 视图，向下为 “月” 视图
						// if (evt.direction == "up" && data.viewType == "month") {
						// console.log('执行了月周切换', data.currentDate, calendar.refreshWeekViews(data.currentDate))
						this.views = calendar.refreshWeekViews(data.currentDate).views;
						this.topValues = calendar.refreshWeekViews(data.currentDate).topValues
						data.viewType = "week";
						trigger = true;
						// } else if (evt.direction == "down" && data.viewType == "week") {
						// 	if (thisd.ltop < 50) {
						// 		this.views = calendar.refreshMonthViews(calendar.currentMonth).views
						// 		// setData = calendar.refreshMonthViews(calendar.currentMonth);
						// 		data.viewType = "month";
						// 		this.topValues = [0, 0, 0];
						// 		trigger = true;
						// 	}
						// }
						this.animate = "animate";
						// this.setDate(setData);
						// 切换视图后上传事件
						if (trigger) {
							this.viewType = data.viewType;
							// this.triggerEvent("viewchange", {
							// 	viewType: setData.viewType
							// })
						}

						calendar.pressmoved = false;
					}
				}
			}),

		},
	};
</script>

<style>
	@import url("./index.css");
</style>
